package com.ro0tsh3ll.tools;

import java.io.IOException;
import java.net.*;
import java.util.Enumeration;

/**
 * Created by Administrator on 2014/6/16.
 */
public class SocketUtil {

    public static Boolean printReachableIP(InetAddress remoteAddr, int port){
        String retIP = null;

        Enumeration<NetworkInterface> netInterfaces;
        try{
            netInterfaces = NetworkInterface.getNetworkInterfaces();
            while(netInterfaces.hasMoreElements()) {
                NetworkInterface ni = netInterfaces.nextElement();
                Enumeration<InetAddress> localAddrs = ni.getInetAddresses();
                while(localAddrs.hasMoreElements()){
                    InetAddress localAddr = localAddrs.nextElement();
                    if(isReachable(localAddr, remoteAddr, port, 500)){
                        retIP = localAddr.getHostAddress();
                        break;
                    }
                }
            }
        } catch(SocketException e) {
            System.out.println(
                    "Error occurred while listing all the local network addresses.");
        }
        if(retIP == null){
            System.out.println("NULL reachable local IP is found!");
            return false;
        }else{
            System.out.println("Reachable local IP is found, it is " + retIP);
            return true;
        }
    }

    public static boolean isReachable(InetAddress localInetAddr, InetAddress remoteInetAddr,
                                      int port, int timeout) {

        boolean isReachable = false;
        Socket socket = null;
        try{
            socket = new Socket();
            // 端口号设置为 0 表示在本地挑选一个可用端口进行连接
            SocketAddress localSocketAddr = new InetSocketAddress(localInetAddr, 0);
            socket.bind(localSocketAddr);
            InetSocketAddress endpointSocketAddr =
                    new InetSocketAddress(remoteInetAddr, port);
            socket.connect(endpointSocketAddr, timeout);
            System.out.println("SUCCESS - connection established! Local: " +
                    localInetAddr.getHostAddress() + " remote: " +
                    remoteInetAddr.getHostAddress() + " port" + port);
            isReachable = true;
        } catch(IOException e) {
            System.out.println("FAILRE - CAN not connect! Local: " +
                    localInetAddr.getHostAddress() + " remote: " +
                    remoteInetAddr.getHostAddress() + " port" + port);
        } finally{
            if(socket != null) {
                try{
                    socket.close();
                } catch(IOException e) {
                    System.out.println("Error occurred while closing socket..");
                }
            }
        }
        return isReachable;
    }

    public static  boolean socket(String ip){
        Socket socket = new Socket();
        SocketAddress address = new InetSocketAddress(ip, 80);
        try {
            socket.connect(address, 400);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(socket("123.205.250.116"));
    }
}
